package com.css.fxfzxczh.modules.synthesizeInfoManage.heaManage.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzxczh.base.response.RestResponse;

import com.css.fxfzxczh.modules.synthesizeInfoManage.earEveManage.entity.EarEveManage;
import com.css.fxfzxczh.modules.synthesizeInfoManage.heaManage.entity.HeaManage;
import com.css.fxfzxczh.modules.synthesizeInfoManage.heaManage.queryParams.HeaManageQuaParams;
import com.css.fxfzxczh.modules.synthesizeInfoManage.heaManage.service.HeaManageService;
import com.css.fxfzxczh.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author
 * @program
 * @description
 * @create 2022/8/8 10:39
 **/
@RestController
@RequestMapping("/heaManage")
@Slf4j
public class HeaManageController {
    @Resource
    HeaManageService heaManageService;
    /**
     * 分页列表
     * @return
     */
    @GetMapping("/getPage/{xsemId}")
    public RestResponse getPage(HttpServletRequest request,@PathVariable("xsemId") String xsemId){
        RestResponse restResponse=null;
        JSONObject jsonObject=new JSONObject();
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize=Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String,Object> page=heaManageService.getPage(curPage,pageSize,xsemId);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(page);
        }catch (Exception e){
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public RestResponse delete(@PathVariable("id") String id){
        RestResponse response = null;
        JSONObject jsonObject=new JSONObject();
        try{
            heaManageService.delete(id);
            jsonObject.put("message","删除成功");
            response = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errormessage = e.getMessage();
            if(StringUtils.isBlank(errormessage)) {
                errormessage = "删除失败";
            }
            log.error(errormessage,e);
            response=RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 修改
     * @param heaManage
     * @return
     */
    @PostMapping("/update")
    public RestResponse update(@RequestBody HeaManage heaManage){
        RestResponse response = null;
        JSONObject jsonObject=new JSONObject();
        try{
            heaManageService.update(heaManage);
            jsonObject.put("message","修改成功");
            response = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errormessage = e.getMessage();
            if(StringUtils.isBlank(errormessage)) {
                errormessage = "修改失败";
            }
            log.error(errormessage,e);
            response=RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 保存
     * @param heaManage
     * @return
     */
    @PostMapping("/save")
    public RestResponse save(@RequestBody HeaManage heaManage){
        RestResponse response = null;
        JSONObject jsonObject=new JSONObject();
        try{
            heaManageService.save(heaManage);
            jsonObject.put("message","保存成功");
            response = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errorMessage = "保存失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 获取指挥部信息
     * @param eventCode
     * @return
     */
    @GetMapping("/getHeadInfo")
    public RestResponse getHeadInfo(String eventCode){
        RestResponse response = null;

        try{
            Map<String,Object> map=heaManageService.getHeadInfo(eventCode);
            response = RestResponse.succeed(map);
        }catch (Exception e){
            String errorMessage = "生成失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 查看
     *
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public RestResponse findById(String id) {
        RestResponse response = null;
        try {
            HeaManage heaManage=heaManageService.findById(id);
            response = RestResponse.succeed(heaManage);
        } catch (Exception e) {
            String errorMessage = "查看失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }
}
